import { FunctionModel } from './Models/FunctionModel'
import { Base } from './Common/Base'
import { DrugTypeServer } from './Servers/DrugTypeServer'
import { FunctionCallBackModel } from './Models/FunctionCallBcakModel'
import { DrugShowModelDto } from './Models/Dtos/DrugShowModelDto'
import { DrugModelServer } from './Servers/DrugModelServer'

const ZONENAME: string = "YaoBox";

let myHandler = async function (event, context, callback, logger) {
  logger.info(event);
  let callBackModel: FunctionCallBackModel = new FunctionCallBackModel();

  try {
    let model = event as FunctionModel;
    switch (model.method) {
      case "Get":
        if (model.methodType === Base.DRUGTYPE_METHODTYPE) {
          const list = await DrugTypeServer.GetDrugTypeListAsync(model.userID)
          callBackModel.data = list;
        } else if (model.methodType === Base.DRUG_METHODTYPE) {
          const list = await DrugModelServer.GetDrugByUserAsync(model.userID);
          callBackModel.data = list;
        }
        break;
      case "Add":
        if (model.methodType == Base.DRUGTYPE_METHODTYPE) {
          let typeName: string = model.param as string;
          let result = await DrugTypeServer.AddDrugTypeFromUserAsync(model.userID, typeName);
          callBackModel.data = result;
        } else if (model.methodType === Base.DRUG_METHODTYPE) {
          let showModel: DrugShowModelDto = model.param as DrugShowModelDto;
          let result = await DrugModelServer.AddDrugAsync(model.userID, showModel);
          callBackModel.data = result;
        }
        break;
      case "Delete":
        if (model.methodType == Base.DRUGTYPE_METHODTYPE) {
          let typeNames: string[] = model.param as string[];
          let result = await DrugTypeServer.DeleteUserDrugTypesAsync(model.userID, typeNames);
          callBackModel.data = result;
        } else if (model.methodType === Base.DRUG_METHODTYPE) {
          let drugID: number = Number.parseInt((model.param as string));
          let result = await DrugModelServer.DeleteDrugAsync(drugID);
          callBackModel.data = result;
        }
        break;
    }
  } catch (e) {
    callBackModel.data = (e as Error).message;
  }
  callback(callBackModel);
};

export { myHandler };